package com.education.wisdom.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.education.common.utils.LogUtil;
import com.education.wisdom.pojo.Answers;
import com.education.wisdom.pojo.Paper;
import com.education.wisdom.pojo.Questions;
import com.education.wisdom.service.IAnswersService;
import com.education.wisdom.service.IPaperService;
import com.education.wisdom.service.IQuestionsService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.education.common.core.BaseController.writeJSON;


/**
 * 答卷 信息操作处理
 *
 * @author ruoyi
 * @date 2018-11-13
 */
@RestController
@CrossOrigin
@Slf4j
@Api(tags = {"答卷 信息操作处理"})
@RequestMapping("/web/answers")
public class FullAnswersController {

    @Autowired
    private IPaperService paperService;

    @Autowired
    private IAnswersService answersService;

    @Autowired
    private IQuestionsService questionsService;

    @GetMapping("/chapterTest")
    public void chapterTest(HttpServletResponse response, String userId, Integer pageSize, Integer pageNum) throws IOException {
        log.info(LogUtil.LogInfo("查询章节测试", new Date()));
        PageHelper.startPage(pageNum, pageSize);
        List<Paper> list = paperService.selectChapterTest(userId);
        for (Paper paper : list) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", paper.getId());
            map.put("userId", userId);
            Answers answers = answersService.selectByPaperId(map);
            paper.setDjStarttime(answers.getDjStarttime());
            paper.setDjEndtime(answers.getDjEndtime());
            long l = paper.getDjEndtime().getTime() - paper.getDjStarttime().getTime();
            long nd = 1000 * 24 * 60 * 60;
            long nh = 1000 * 60 * 60;
            long nm = 1000 * 60;
            long min = l % nd % nh / nm;
            paper.setYonshi(min);
            paper.setDefen(answers.getDjScore().toString());
        }
        writeJSON(new PageInfo<>(list), response, "200", "成功");
    }

    @GetMapping("/vocationalSkillTest")
    public void vocationalSkillTest(HttpServletResponse response, String userId, Integer pageSize, Integer pageNum) throws IOException {
        log.info(LogUtil.LogInfo("查询自己答过的技能测试", new Date()));
        if (pageNum != null && pageSize != null){
            PageHelper.startPage(pageNum,pageSize);
        }
        List<Paper> list = paperService.selectVocationalSkillTest(userId);
        for (Paper paper : list) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", paper.getId());
            map.put("userId", userId);
            Answers answers = answersService.selectByPaperId(map);
            paper.setDjStarttime(answers.getDjStarttime());
            paper.setDjEndtime(answers.getDjEndtime());
            long l = paper.getDjEndtime().getTime() - paper.getDjStarttime().getTime();
            long nd = 1000 * 24 * 60 * 60;
            long nh = 1000 * 60 * 60;
            long nm = 1000 * 60;
            long min = l % nd % nh / nm;
            paper.setYonshi(min);
            paper.setDefen(answers.getDjScore().toString());
        }
        writeJSON(new PageInfo<>(list), response, "200", "成功");
    }

    @GetMapping("/answerRecord")
    public void answerRecord(HttpServletResponse response, String userId, Integer id) throws IOException {
        int j = 0;
        log.info(LogUtil.LogInfo("查询答题记录", new Date()));
        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        map.put("userId", userId);
        Answers answers = answersService.selectByPaperId(map);
        if (answers != null) {
            Paper paper = paperService.selectPaperById(answers.getDjPaperid());
            String djContent = answers.getDjContent();
            String[] split = djContent.split(",");
            String[] o = answersService.selectByIds(split);
            List<Questions> list = questionsService.selectByPaperId(id);
            for (int i = 0; i < list.size(); i++) {
                list.get(i).setOwn(o[j].toString());
                j++;
                list.get(i).setStCteater(paper.getSjIntroduction());
            }
            writeJSON(list, response, "200", "成功");
        }
    }
}